//1.引入
//引入vue-router及相关的
import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter);

const router = new VueRouter({
    //其他配置

    //路由器配置
    routes: [
        /*=============== 登录页面 =================*/
        {
            path: '/login',
            component: () => import('@/views/login/index')
        },
        /*=============== 主页 =================*/
        {
            path: '/',
            component: () => import('@/components/TabBar'),
            children: [
                // 设备页面
                {
                    path: 'facility',
                    component: () => import('@/views/facility/index'),
                    children: [
                        {
                            path: 'facistate',
                            component: () => import('@/views/facility/components/facistate'),
                            children: [
                                {
                                    path: 'cards',
                                    component: () => import('@/views/facility/components/Cards')
                                },
                            ]
                        },
                    ]
                },
                //
                {
                    path:'DarkModelPage',
                    component: ()=> import('@/views/DarkModelPage'),
                },
                {
                    path:'Assign',
                    component: ()=> import('@/views/staff/components/Assign'),
                },
                {
                    path: 'worksheet',
                    component: () => import('@/views/worksheet/index'),
                    children: [
                        {
                            path: 'sheetData',
                            component: () => import('@/views/worksheet/components/Card')
                        }
                    ]
                },
                // 人员页面
                {
                    path: 'staff',
                    component: () => import('@/views/staff/index'),
                    children: [
                        {
                            path: 'staffData',
                            component: () => import('@/views/staff/components/Cards')
                        }
                    ]
                },
                //我的页面
                {
                    path: 'mine',
                    component: () => import('@/views/mine/index')
                },
                //添加工单页面
                {
                    path: 'addForm',
                    component: () => import('@/views/worksheet-form/index')
                },
                //
                {
                    path: 'etc',
                    component: () => import('@/views/worksheet-form/components/blank')
                },
                // 工单详情页
                {
                    path: 'sheetDetails',
                    component: () => import('@/views/worksheet-details/index')
                },
                //设备详情页
                {
                    path: 'Device',
                    component: () => import('@/views/facility/components/Device'),
                },
                //人员分配工单页
                {
                    path: 'Assign',
                    component: () => import('@/views/staff/components/Assign'),
                },
                {
                    path: 'updateForm',
                    component: () => import('@/views/mine/updateForm')
                },
                {
                    path: 'updateMaintain',
                    component: () => import('@/views/mine/updateMaintain')
                },
                {
                    path: 'deviceData',
                    component: () => import('@/views/facility/components/deviceData')
                }
            ]
        },
        /*============ 404页面 ===============*/
        {
            path: "*",
            component: () => import('@/components/404')
        }
    ],

})

// 解决vue-router报NavigationDuplicated: Avoided redundant navigation to current location 的问题
const originalPush = VueRouter.prototype.push;  //重写push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
};
const originalReplace = VueRouter.prototype.replace;  //重写replace
VueRouter.prototype.replace = function replace(location) {
    return originalReplace.call(this, location).catch(err => err)
};

//路由守卫
router.beforeEach((to, from, next) => {
    if (to.path === '/login') next()
    const admin = sessionStorage["id"]
    // console.log(sessionStorage.id === admin)
    if (!admin && to.path !== '/login') return next("/login")  // 强制退回到登录页面
    next()   //否则放行
})

export default router;
